const orderService = require('./../services/order.service');

module.exports = {
    /**
     * 预购记录添加
     */
    async addOrder(ctx) {
        let result = {
            result: false,
            message: ''
        };
        let session = ctx.session;
        if (session.isLogin) { 
            let formData = ctx.request.body;
            let res = await orderService.addOrder({
                buyer_id: formData.buyer_id,
                seller_id: formData.seller_id,
                book_id: formData.book_id,
                state: formData.state
            });
            if (res && res.affectedRows) {
                result.result = true;
                result.message = '预购记录添加成功';
            }
        }else {
            result.message = '请先登录';
        }
        
        ctx.body = result;
    },
    /**
     * 预购记录获取
     * 
     */
    async getOrder(ctx) {
        let result = {
            result: false,
            message: '',
            books_id: []
        };
        let user_id = ctx.query.user_id;
        let res = await orderService.getOrder({ user: user_id });
        if (res.length > 0) {
            let ids = [];
            for (let item of res) {
                ids.push(item.book_id);
            }
            result.result = true;
            result.books_id = ids;
        } else {
            result.message = '暂无预购记录';
        }
        ctx.body = result;
    },
    /**
     * 预购记录修改
     */
    async modifyOrder(ctx) {
        let result = {
            result: false,
            message: '预购记录修改失败'
        };
        let formData = ctx.request.body;
        let res = await orderService.modifyOrder({
            book_id: formData.book_id,
            buyer: formData.buyer_id
        });
        if (res && res.affectedRows) {
            result.result = true;
            result.message = '预购记录修改成功';
        }
        ctx.body = result;
    },
    /**
     * 预购人员获取
     * 
     */
    async getOrderPeople(ctx) {
        let result = {
            result: false,
            message: '',
            buyers_id: []
        };
        let book_id = ctx.query.book_id;
        let res = await orderService.getOrderPeople({ book_id: book_id });

        if (res.length > 0) {
            let ids = [];
            for (let item of res) {
                ids.push(item.buyer_id);
            }
            result.result = true;
            result.buyers_id = ids;
        } else {
            result.message = '暂无预购人员';
        }
        ctx.body = result;
    },
    /**
     * 交易对象获取
     * 
     */
    async getTradePeople(ctx) {
        let result = {
            result: false,
            message: '',
            buyer_id: ''
        };
        let book_id = ctx.query.book_id;
        let res = await orderService.getTradePeople({ book_id: book_id });

        if (res) {
            result.result = true;
            result.buyer_id = res.buyer_id;
        } else {
            result.message = '暂无交易对象';
        }
        ctx.body = result;
    },
    /**
     * 在售记录获取
     */
    async getForSale(ctx) {
        let result = {
            result: false,
            message: '',
            books_id: []
        };
        let user_id = ctx.query.user_id;
        let res = await orderService.getForSale({ user_id: user_id });
        
        if (res.length > 0) {
            let ids = [];
            for (let item of res) {
                ids.push(item.book_id);
            }
            result.result = true;
            result.books_id = ids;
        } else {
            result.message = '暂无在售记录';
        }
        ctx.body = result;
    }
}